var cunzhuJSon={};
var cunzhuJSonstr="{}";
var datevar="";
var curintnav=5;
///数字格式化成时间/////
function gestTIME(num){
	var shi=Math.floor(num/60);
	shi=shi>9?shi:"0"+shi;
	var fen=num%60;
	fen=fen>9?fen:"0"+fen;
	return shi+":"+fen;
}
//////添加时间///////
function addtimeslect(eq,ayr,show){
	eq=eq||0;
	ayr=ayr||[];
	
	var timelist=jQuery(".timelist");
	var danwei=0;
	if(eq==0){
		danwei=60;
		timelist.removeClass("scrolly");
	}else{
		if(eq==1)danwei=30;
		if(eq==2)danwei=15;
		timelist.addClass("scrolly");
	}
	
	if(eq>2){
		timelist.html("");
		jQuery(".timeslectH a").removeClass("curent").eq(0).addClass("curent");
		timelist.removeClass("scrolly");
		return;	
	}else{
		jQuery(".timeslectH a").removeClass("curent").eq(eq).addClass("curent");	
	}
	var htrmlst="";
	for(var i=0;i<1440;i+=danwei){
		var htmlstr=gestTIME(i);
		var clasname="";
		for(var j=0;j<ayr.length;j++){
			if(htmlstr==ayr[j]){
				clasname=" class=\"current\"";
			}
		}
		htrmlst+="<a"+clasname+" href=\"javascript:void(0)\">"+htmlstr+"</a>";
	}
	if(curintnav!=eq || show){
		timelist.html(htrmlst);
		curintnav=eq;
	}
}

//////设置input值///////
function setshowdatetext(){
	var valstr="";
	var ayrstr="";
	jQuery(".timelist a.current").each(function(index, element) {
		if(ayrstr!=""){
			ayrstr+=","	
		}
		ayrstr+=jQuery(this).text();
	});
	if(ayrstr!=""){
		ayrstr=","+ayrstr;
	}
	for(var o in cunzhuJSon){
		if(valstr!=""){
			valstr+="|";	
		}
		
		if(cunzhuJSon[o].b.length>0){
			ayrstr=","+cunzhuJSon[o].b.toString();
		}
		
		valstr+=(o+","+cunzhuJSon[o].a+ayrstr);
	}
	jQuery(".showdatetext").val(valstr);
}
	
////////弹出窗口时设置显示状态////////
function tanshowset(){
	jQuery(".item.current").removeClass("current");
	var itemo=jQuery(".dayItem .item");
	itemo.each(function(index, element) {
        var thio=jQuery(this);
		var thidate=thio.attr("data-date");
		if(cunzhuJSon[thidate]){
			thio.addClass("current");	
		}
		var lastcur=jQuery(".item.current:last");
		if(lastcur.length>0){
			datevar=lastcur.attr("data-date");
			//addtimeslect(cunzhuJSon[datevar].a,cunzhuJSon[datevar].b);
		}else{
			datevar="";	
			//addtimeslect(3)
		}
    });	
}


/////////显示弹窗//////////
function tanriliContentS(){
	jQuery(".tanriliContent").show();
	tanshowset();
	jQuery(".tanriliContent").show();	
}

///////关闭弹窗///////
function tanriliContentH(){
	jQuery(".tanriliContent").hide();
	
}



var datetimenum=0;
var datetimenumq=500;

var datetimenuma=0;
var datetimenumqa=500;

jQuery(function(){

	setshowdatetext();/////设置input初始值///////
	
	/////日期绑定事件//////
	jQuery("body").on("click",".dayItem .item",function(){
		datetimenumq=(new Date()).getTime()
		if(Math.abs(datetimenumq-datetimenum)<100){
			return;
		}
		datetimenum=datetimenumq;
		var thio=jQuery(this);
		if(thio.hasClass("gq")) return;
		thio.toggleClass("current");
		var thisdate=thio.attr("data-date");
		if(thio.hasClass("current")){
			datevar=thisdate;
			cunzhuJSon[datevar]={a:0,b:[]};
			if(jQuery(".dayItem .item.current").length==1){
				addtimeslect(0,[],true);
			}else{
				addtimeslect(0);
			}
		}else{
			delete cunzhuJSon[thisdate]
			var lastcur=jQuery(".item.current:last");
			if(lastcur.length>0){
				datevar=lastcur.attr("data-date");
				addtimeslect(cunzhuJSon[datevar].a,cunzhuJSon[datevar].b);
			}else{
				datevar="";	
				addtimeslect(3)
			}
		}
		setshowdatetext();
		//alert(datevar)
	})
	
	/////时间绑定事件/////////
	jQuery("body").on("click",".timelist a",function(){
		datetimenumqa=(new Date()).getTime()
		if(Math.abs(datetimenumqa-datetimenuma)<100){
			return;
		}
		datetimenuma=datetimenumqa;
		var thio=jQuery(this);
		if(datevar=="") return;
		thio.toggleClass("current");
		cunzhuJSon[datevar].a=jQuery(".timeslectH a.curent").index();
		var addary=[];
		jQuery(".timelist a.current").each(function(index, element) {
            addary.push(jQuery(this).text());
        });
		cunzhuJSon[datevar].b=addary;
		setshowdatetext();
	})
	//////切换单位///////
	jQuery(".timeslectH a").click(function(){
		//alert(datevar=="")
		if(datevar=="") return;
		var indeshix=jQuery(this).index();
		var ysindex=jQuery(".timeslectH a.curent").index();
		if(ysindex==indeshix) return;
		addtimeslect(indeshix,[]);
	});
	
	
	///////确定///////
	jQuery(".timeFoot a.queding").click(function(){
		cunzhuJSonstr=JSON.stringify(cunzhuJSon);
		tanriliContentH();
	});
	////取消////
	jQuery(".timeFoot a.quxiao").click(function(){
		cunzhuJSon=JSON.parse(cunzhuJSonstr);
		setshowdatetext();
		tanriliContentH();
	});
	
	//////编辑按钮///////
	jQuery(".showtianchu").click(tanriliContentS);	
	
})
document.ontouchmove=function(e){
			e.preventDefault();
			return false;
		}

		var dataJSon=[{date:20160606,num:5,money:5700},{date:20160620,num:5,money:5300},{date:20160530,num:3,money:6200},{date:20160703,num:0,money:6100},{date:20160715,num:10,money:6100}]

		jQuery(document).ready(function(e) {
			CalendarHandler.initialize();
		});
		
		function gesDt(num){
			if(num>9) return num;
			return "0"+num;
		}

		var CalendarHandler = {
			currentYear: 0,
			currentMonth: 0,
			isRunning: false,
			showYearStart:2009,
			tag:0,
			initialize: function() {
				$calendarItem = this.CreateCalendar(0, 0, 0);
				jQuery("#Container").append($calendarItem);
				tanshowset();
				jQuery("#context").css("height", jQuery("#CalendarMain").height() - 65 + "px");
				jQuery("#center").css("height", jQuery("#context").height() - 30 + "px");
				jQuery("#selectYearDiv").css("height", jQuery("#context").height() - 30 + "px").css("width", jQuery("#context").width() + "px");
				jQuery("#selectMonthDiv").css("height", jQuery("#context").height() - 30 + "px").css("width", jQuery("#context").width() + "px");
				jQuery("#centerCalendarMain").css("height", jQuery("#context").height() - 30 + "px").css("width", jQuery("#context").width() + "px");

				$calendarItem.css("height", jQuery("#context").height() - 30 + "px"); //.css("visibility","hidden");
				jQuery("#Container").css("height", "0px").css("width", "0px").css("margin-left", jQuery("#context").width() / 2 + "px").css("margin-top", (jQuery("#context").height() - 30) / 2 + "px");
				jQuery("#Container").animate({
					width: jQuery("#context").width() + "px",
					height: (jQuery("#context").height() - 30) * 2 + "px",
					marginLeft: "0px",
					marginTop: "0px"
				}, 300, function() {
					$calendarItem.css("visibility", "visible");
				});
				jQuery(".dayItem").css("width", jQuery("#context").width() + "px");
				var itemPaddintTop = jQuery(".dayItem").height() / 6;
				jQuery(".item").css({
					"width": jQuery(".week").width() / 7 + "px",
					"line-height": itemPaddintTop + "px",
					"height": itemPaddintTop + "px"
				});
				jQuery(".currentItem>a").css("margin-left", (jQuery(".item").width() - 25) / 2 + "px").css("margin-top", (jQuery(".item").height() - 25) / 2 + "px");
				jQuery(".week>h3").css("width", jQuery(".week").width() / 7 + "px");
				this.RunningTime();
			},
			CreateSelectYear: function(showYearStart) {
				CalendarHandler.showYearStart=showYearStart;
				jQuery(".currentDay").show();
				jQuery("#selectYearDiv").children().remove();
				var yearindex = 0;
				for (var i = showYearStart; i < showYearStart+12; i++) {
					yearindex++;
					if(i==showYearStart){
						$last=jQuery("<div>往前</div>");
						jQuery("#selectYearDiv").append($last);
						$last.click(function(){
							CalendarHandler.CreateSelectYear(CalendarHandler.showYearStart-10);
						});
						continue;
					}
					if(i==showYearStart+11){
						$next=jQuery("<div>往后</div>");
						jQuery("#selectYearDiv").append($next);
						$next.click(function(){
							CalendarHandler.CreateSelectYear(CalendarHandler.showYearStart+10);
						});
						continue;
					}
					
					if (i == this.currentYear) {
						$yearItem=jQuery("<div class=\"currentYearSd\" id=\"" + yearindex + "\">" + i + "</div>")
					
					}
					else{
						 $yearItem=jQuery("<div id=\"" + yearindex + "\">" + i + "</div>");
					}
					jQuery("#selectYearDiv").append($yearItem);
					$yearItem.click(function(){
						$calendarItem=CalendarHandler.CreateCalendar(Number(jQuery(this).html()),1,1);
						jQuery("#Container").append($calendarItem);
						tanshowset();
						CalendarHandler.CSS()
					    CalendarHandler.isRunning = true;
					    jQuery(jQuery("#Container").find(".dayItem")[0]).animate({
						height: "0px"
					    }, 300, function() {
						jQuery(this).remove();
						CalendarHandler.isRunning = false;
					    });
						jQuery("#centerMain").animate({
						marginLeft: -jQuery("#center").width() + "px"
					}, 500);
					});
					if (yearindex == 1 || yearindex == 5 || yearindex == 9) jQuery("#selectYearDiv").find("#" + yearindex).css("border-left-color", "#fff");
					if (yearindex == 4 || yearindex == 8 || yearindex == 12) jQuery("#selectYearDiv").find("#" + yearindex).css("border-right-color", "#fff");
					
				}
				jQuery("#selectYearDiv>div").css("width", (jQuery("#center").width() - 4) / 4 + "px").css("line-height", (jQuery("#center").height() - 4) / 3 + "px");
				jQuery("#centerMain").animate({
					marginLeft: "0px"
				}, 300);
			},
			CreateSelectMonth: function() {
				jQuery(".currentDay").show();
				jQuery("#selectMonthDiv").children().remove();
				for (var i = 1; i < 13; i++) {
					if (i == this.currentMonth) $monthItem=jQuery("<div class=\"currentMontSd\" id=\"" + i + "\">" + i + "月</div>");
					else  $monthItem=jQuery("<div id=\"" + i + "\">" + i + "月</div>");
					jQuery("#selectMonthDiv").append($monthItem);
					$monthItem.click(function(){
						$calendarItem=CalendarHandler.CreateCalendar(CalendarHandler.currentYear,Number(jQuery(this).attr("id")),1);
						jQuery("#Container").append($calendarItem);
						tanshowset();
						CalendarHandler.CSS()
					    CalendarHandler.isRunning = true;
					    jQuery(jQuery("#Container").find(".dayItem")[0]).animate({
						height: "0px"
					    }, 300, function() {
						jQuery(this).remove();
						CalendarHandler.isRunning = false;
					    });
						jQuery("#centerMain").animate({
						marginLeft: -jQuery("#center").width() + "px"
					}, 500);
					});
					if (i == 1 || i == 5 || i == 9) jQuery("#selectMonthDiv").find("#" + i).css("border-left-color", "#fff");
					if (i == 4 || i == 8 || i == 12) jQuery("#selectMonthDiv").find("#" + i).css("border-right-color", "#fff");
				}
				jQuery("#selectMonthDiv>div").css("width", (jQuery("#center").width() - 4) / 4 + "px").css("line-height", (jQuery("#center").height() - 4) / 3 + "px");
				jQuery("#centerMain").animate({
					marginLeft: -jQuery("#center").width() * 2 + "px"
				}, 300);
			},
			IsRuiYear: function(aDate) {
				return (0 == aDate % 4 && (aDate % 100 != 0 || aDate % 400 == 0));
			},
			CalculateWeek: function(y, m, d) {
				var arr = "7123456".split("");
				with(document.all) {
					var vYear = parseInt(y, 10);
					var vMonth = parseInt(m, 10);		
					var vDay = parseInt(d, 10);
				}
				var week =arr[new Date(y,m-1,vDay).getDay()];
				return week;
			},
			CalculateMonthDays: function(m, y) {
				var mDay = 0;
				if (m == 0 || m == 1 || m == 3 || m == 5 || m == 7 || m == 8 || m == 10 || m == 12) {
					mDay = 31;
				} else {
					if (m == 2) {
						//判断是否为芮年
						var isRn = this.IsRuiYear(y);
						if (isRn == true) {
							mDay = 29;
						} else {
							mDay = 28;
						}
					} else {
						mDay = 30;
					}
				}
				return mDay;
			},
			CreateCalendar: function(y, m, d) {
				$dayItem = jQuery("<div class=\"dayItem\"></div>");
				//获取当前月份的天数
				var nowDate = new Date();
				if(y==nowDate.getFullYear()&&m==nowDate.getMonth()+1||(y==0&&m==0))
				jQuery(".currentDay").hide();
				var nowYear = y == 0 ? nowDate.getFullYear() : y;
				this.currentYear = nowYear;
				var nowMonth = m == 0 ? nowDate.getMonth() + 1 : m;
				this.currentMonth = nowMonth;
				var nowDay = d == 0 ? nowDate.getDate() : d;
				jQuery(".selectYear").html(nowYear + "年");
				jQuery(".selectMonth").html(nowMonth + "月");
				var nowDaysNub = this.CalculateMonthDays(nowMonth, nowYear);
				var nowWeek = parseInt(this.CalculateWeek(nowYear, nowMonth, 1));
				var lastMonthDaysNub = this.CalculateMonthDays((nowMonth - 1), nowYear);

				if (nowWeek != 0) {
					//生成上月剩下的日期
					for (var i = (lastMonthDaysNub - (nowWeek - 1)); i < lastMonthDaysNub; i++) {
						var tians=(i + 1)>9?(i + 1):"0"+(i + 1);
						var nians=parseInt(nowYear);
						var yueshs=parseInt(nowMonth);
						nians=yueshs==1?nians-1:nians;
						yueshs=yueshs-1<1?12:yueshs-1;
						yueshs=yueshs>9?yueshs:"0"+yueshs;
						var callJS=retinfostr(""+nians+yueshs+tians);
						var clastp="";
						var textstr="";
						if(callJS.strin!=""){
							if(callJS.isqg){
								clastp=" g";
							}
							textstr=callJS.strin;
						}
						$dayItem.append("<div data-date=\""+gesDt(nowYear)+"-"+gesDt(nowMonth)+"-"+gesDt(i + 1)+"\" class=\"item gq"+clastp+" lastItem\"><a>" + (i + 1) + "</a>"+textstr+"</div>");
					}
				}

				//生成当月的日期
				for (var i = 0; i < nowDaysNub; i++) {
						var tians=(i + 1)>9?(i + 1):"0"+(i + 1);
						var nians=parseInt(nowYear);
						var yueshs=parseInt(nowMonth);
						yueshs=yueshs>9?yueshs:"0"+yueshs;
						var callJS=retinfostr(""+nians+yueshs+tians);
						var clastp="";
						var textstr="";
						if(callJS.strin!=""){
							if(callJS.isqg){
								clastp=" g";
							}
							textstr=callJS.strin;
						}
					if (i == (nowDay - 1)) $dayItem.append("<div data-date=\""+gesDt(nowYear)+"-"+gesDt(nowMonth)+"-"+gesDt(i + 1)+"\" class=\"item"+clastp+" currentItem\"><a>" + (i + 1) + "</a>"+textstr+"</div>");
					else $dayItem.append("<div data-date=\""+gesDt(nowYear)+"-"+gesDt(nowMonth)+"-"+gesDt(i + 1)+"\" class=\"item"+clastp+"\"><a>" + (i + 1) + "</a>"+textstr+"</div>");
				}

				//获取总共已经生成的天数
				var hasCreateDaysNub = nowWeek + nowDaysNub;
				//如果小于42，往下个月推算
				if (hasCreateDaysNub < 42) {
					for (var i = 0; i <= (42 - hasCreateDaysNub); i++) {
						var tians=(i + 1)>9?(i + 1):"0"+(i + 1);
						var nians=parseInt(nowYear);
						var yueshs=parseInt(nowMonth);
						nians=yueshs==12?nians+1:nians;
						yueshs=yueshs+1>12?1:yueshs+1;
						yueshs=yueshs>9?yueshs:"0"+yueshs;
						var callJS=retinfostr(""+nians+yueshs+tians);
						var clastp="";
						var textstr="";
						if(callJS.strin!=""){
							if(callJS.isqg){
								clastp=" g";
							}
							textstr=callJS.strin;
						}
						$dayItem.append("<div data-date=\""+gesDt(nowYear)+"-"+gesDt(nowMonth+1)+"-"+gesDt(i + 1)+"\" class=\"item"+clastp+" lastItem\"><a>" + (i + 1) + "</a>"+textstr+"</div>");
					}
				}

				return $dayItem;
			},
			CSS: function() {
				var itemPaddintTop = jQuery(".dayItem").height() / 6;
				jQuery(".item").css({
					"width": jQuery(".week").width() / 7 + "px",
					"line-height": itemPaddintTop + "px",
					"height": itemPaddintTop + "px"
				});
				jQuery(".currentItem>a").css("margin-left", (jQuery(".item").width() - 25) / 2 + "px").css("margin-top", (jQuery(".item").height() - 25) / 2 + "px");
			},
			CalculateNextMonthDays: function() {
				if (this.isRunning == false) {
					jQuery(".currentDay").show();
					var m = this.currentMonth == 12 ? 1 : this.currentMonth + 1;
					var y = this.currentMonth == 12 ? (this.currentYear + 1) : this.currentYear;
					var d = 0;
					var nowDate = new Date();
					if (y == nowDate.getFullYear() && m == nowDate.getMonth() + 1) d = nowDate.getDate();
					else d = 1;
					$calendarItem = this.CreateCalendar(y, m, d);
					jQuery("#Container").append($calendarItem);
					tanshowset();
					this.CSS();
					this.isRunning = true;
					jQuery(jQuery("#Container").find(".dayItem")[0]).animate({
						height: "0px"
					}, 300, function() {
						jQuery(this).remove();
						CalendarHandler.isRunning = false;
					});
				}
			},
			CalculateLastMonthDays: function() {
				if (this.isRunning == false) {
					jQuery(".currentDay").show();
					var nowDate = new Date();					
					var m = this.currentMonth == 1 ? 12 : this.currentMonth - 1;
					var y = this.currentMonth == 1 ? (this.currentYear - 1) : this.currentYear;
					var d = 0;
					
					if (y == nowDate.getFullYear() && m == nowDate.getMonth() + 1) d = nowDate.getDate();
					else d = 1;
					$calendarItem = this.CreateCalendar(y, m, d);
					jQuery("#Container").append($calendarItem);
					tanshowset();
					var itemPaddintTop = jQuery(".dayItem").height() / 6;
					this.CSS();
					this.isRunning = true;
					jQuery(jQuery("#Container").find(".dayItem")[0]).animate({
						height: "0px"
					}, 300, function() {
						jQuery(this).remove();
						CalendarHandler.isRunning = false;
					});
				}
			},
			CreateCurrentCalendar: function() {
				if (this.isRunning == false) {
					jQuery(".currentDay").hide();
					$calendarItem = this.CreateCalendar(0, 0, 0);
					jQuery("#Container").append($calendarItem);
					tanshowset();
					this.isRunning = true;
					jQuery(jQuery("#Container").find(".dayItem")[0]).animate({
						height: "0px"
					}, 300, function() {
						jQuery(this).remove();
						CalendarHandler.isRunning = false;
					});
					this.CSS();
					jQuery("#centerMain").animate({
						marginLeft: -jQuery("#center").width() + "px"
					}, 500);
				}
			},
			RunningTime: function() {
				var mTiming = setInterval(function() {
					var nowDate = new Date();
					var h=nowDate.getHours()<10?"0"+nowDate.getHours():nowDate.getHours();
					var m=nowDate.getMinutes()<10?"0"+nowDate.getMinutes():nowDate.getMinutes();
					var s=nowDate.getSeconds()<10?"0"+nowDate.getSeconds():nowDate.getSeconds();
					var nowTime = h + ":" + m + ":" + s;
					jQuery("#footNow").html("本地时间 "+nowTime);
				}, 1000);

			}
		}
		
		
		function retinfostr(str){
			var reob={strin:"",isqg:false};
			for(var i=0;i<dataJSon.length;i++){
				var thdate=dataJSon[i].date;
				if(str==thdate){
					var ft;
					var nums=dataJSon[i].num;
					if(nums>0){
						ft="剩余"+nums;
						reob.isqg=false;
					}else{
						ft="抢光啦";
						reob.isqg=true;
					}
					reob.strin="<span>￥"+dataJSon[i].money+"</span><font>"+ft+"</font>"
				}
			}
			return reob;
		}
